//
//  CaseManageController.m
//  KMedicine
//
//  Created by 曹牧 on 16/7/5.
//  Copyright © 2016年 caomu. All rights reserved.
//

#import "CaseManageController.h"
#import "YYHeadView.h"
#import "AddCaseButton.h"
#import "AddCaseController.h"

#import "AKuCaseManageCell.h"
#import "YYListModel.h"


@interface CaseManageController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)UITableView *tv_case;
@property (nonatomic, strong)YYHeadView *headView;
@property (nonatomic, strong)AddCaseButton *btn_case;

@property (nonatomic, strong) NSArray *dataArray;


@end

@implementation CaseManageController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 初始化界面
    [self setupUI];
}

#pragma mark
#pragma mark 初始化界面
- (void)setupUI {
    
    //初始化底部 添加病例 按钮
    AddCaseButton *btn_case = [[AddCaseButton alloc] init];
    [btn_case setBackgroundImage:[UIImage imageNamed:@"nav"] forState:UIControlStateNormal];
    [btn_case setTitle:@"添加病例" forState:UIControlStateNormal];
    self.btn_case = btn_case;
    [btn_case addTarget:self action:@selector(addCase:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn_case];
    
    // 初始化 tableView
    UITableView *tbView = [[UITableView alloc] init];
    self.tv_case = tbView;
    self.tv_case.delegate = self;
    self.tv_case.dataSource = self;
    
    [self.tv_case registerClass:[AKuCaseManageCell class] forCellReuseIdentifier:@"cell"];
    [self.tv_case registerClass:[AKuCaseManageCell class] forCellReuseIdentifier:@"firstCell"];
    
    // 禁用自动偏移
    self.modalPresentationCapturesStatusBarAppearance = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.extendedLayoutIncludesOpaqueBars = NO;
    
    // MARK:tableView的背景颜色
    self.tv_case.tableFooterView = [UIView new];
    [self.view addSubview:self.tv_case];
    
    // 初始化headView
    self.headView = [[YYHeadView alloc] init];

    [self.view addSubview:self.headView];

    // MARK: 添加到 view后,设置约束
    [self setLayout];

}

#pragma mark
#pragma mark 添加病例按钮 监听方法
- (void)addCase:(UIButton *)sender{
    
    AddCaseController *addController = [[AddCaseController alloc] init];
    addController.view.backgroundColor = [UIColor whiteColor];
    addController.plistName = @"YYCase.plist";
    addController.navTitle = [sender currentTitle];
    [self.navigationController pushViewController:addController animated:YES];
    
}

#pragma mark
#pragma mark 设置约束
- (void)setLayout {
    
    
    
    
    // MARK:headView 的约束
    [self.headView makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.offset(0);
        make.height.offset(150);
        
    }];
    
    [self.btn_case makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.offset(15);
        make.right.bottom.offset(-15);
        make.height.offset(40);
    }];
    
    // MARK:tableView的约束
    [self.tv_case makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.offset(0);
        make.top.equalTo(self.headView.bottom).offset(20);
        make.bottom.equalTo(self.btn_case.top).offset(-20);
    }];
    
}

- (void)viewWillLayoutSubviews {
    
    [super viewWillLayoutSubviews];
    
    self.dataArray = [NSKeyedUnarchiver unarchiveObjectWithFile:kFilePath(@"user.data")];
    
    [self.tv_case reloadData];
}





//+++++++++++++++++++++++++++++++++++++++++++++++
#pragma mark
#pragma mark tableView 代理方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *cellIdextifier = @"cell";
    
    if (indexPath.row == 0) {
        
        cellIdextifier = @"firstCell";
    }
    
    AKuCaseManageCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdextifier forIndexPath:indexPath];
    
    // 倒序 让最新的显示到 第一
    YYListModel *model = self.dataArray[self.dataArray.count - indexPath.row - 1];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.modeCell = model;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++



@end
